﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SJZD_TypeManage : ATPages
{
    ZSql zsql = new ZSql();

    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            hfxs.Value = Request.QueryString["moduid"].ToString();
            lblwz.Text = f.getModuleidAdd(hfxs.Value);

            gdata();
            initData();

            initType1();
        }
    }
    protected void lnkbtnFlag_Click(object sender, EventArgs e)
    {
        string StaffId = (sender as LinkButton).CommandArgument.ToString();
        Response.Redirect("StaffAdd.aspx?StaffId=" + StaffId + "&moduid=" + hfxs.Value + "&action=flag");

    }

    protected void lnkbtnEdit_Click(object sender, EventArgs e)
    {
        string StaffId = (sender as LinkButton).CommandArgument.ToString();
        Response.Redirect("StaffAdd.aspx?StaffId=" + StaffId + "&moduid=" + hfxs.Value + "&action=update");

    }
    protected void btndel_Click(object sender, EventArgs e)
    {
        ZSql del = new ZSql();
        int n = 0;
        if (this.GvDataStaff.Rows.Count > 0)
        {
            for (int i = 0; i < GvDataStaff.Rows.Count; i++)
            {
                CheckBox ck = GvDataStaff.Rows[i].Cells[8].FindControl("chkDelete") as CheckBox;
                if (ck.Checked)
                {

                    del.Open("delete CS_Staff  where StaffId=" + this.GvDataStaff.Rows[i].Cells[0].Text);
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('删除成功');", true);
                }
                else
                {
                    n++;
                }

                if (n == this.GvDataStaff.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }

            }
        }
    }




    protected void btnSelec_Click(object sender, EventArgs e)
    {
        ZSql selec = new ZSql();
        string sql = "select s.StaffId,s.AuditState, s.StaffName,s.StaffWork,s.StaffTel,s.StaffPercentage,s.StaffPrice,s.StaffSalary,s.SWage,convert(char(10),s.WageDate,120) as WageDate,s.EditUserName,convert(char(10),s.EditDate,120) as EditDate,s.StaffOrder,w.WTitle,d.Deptname,(select count(0) from CS_Sales where s.StaffId=Saleman)as salesNum from CS_Staff as s,CS_WorkTitle as w,Department as d where w.WTId=s.StaffWork and d.Deptid=s.Deptid";
        if (txtselecName.Text == "" && ddrDepartment.SelectedValue == "0" && StaffWork.SelectedValue == "0")
        {
            sql += "";
        }
        else if (ddrDepartment.SelectedValue == "0" && StaffWork.SelectedValue == "0")
        {
            sql += " and s.StaffName like'%" + Common.FormatParameter(txtselecName.Text) + "%'";

        }
        else if (txtselecName.Text == "" && ddrDepartment.SelectedValue == "0")
        {
            sql += " and w.WTId=" + StaffWork.SelectedValue + "";

        }
        else if (txtselecName.Text == "" && StaffWork.SelectedValue == "0")
        {
            sql += " and d.Deptid=" + ddrDepartment.SelectedValue + "";


        }
        else if (txtselecName.Text == "" && StaffWork.SelectedValue != "0" && ddrDepartment.SelectedValue != "0")
        {
            sql += " and w.WTId=" + StaffWork.SelectedValue + " and d.Deptid=" + ddrDepartment.SelectedValue + "";

        }
        else if (txtselecName.Text != "" && ddrDepartment.SelectedValue != "0" && StaffWork.SelectedValue == "0")
        {
            sql += " and s.StaffName like'%" + Common.FormatParameter(txtselecName.Text) + "%' and d.Deptid=" + ddrDepartment.SelectedValue + "";
        }
        else if (txtselecName.Text != "" && StaffWork.SelectedValue != "0" && ddrDepartment.SelectedValue == "0")
        {
            sql += " and s.StaffName like'%" + Common.FormatParameter(txtselecName.Text) + "%' and w.WTId=" + StaffWork.SelectedValue + "";
        }
        else
        {
            sql += " and s.StaffName like'%" + Common.FormatParameter(txtselecName.Text) + "%' and w.WTId=" + StaffWork.SelectedValue + " and d.Deptid=" + ddrDepartment.SelectedValue + "";

        }
        if (!string.IsNullOrEmpty(AuditState.SelectedValue)) 
        {
            sql+=" and s.AuditState="+AuditState.SelectedValue;
        }
        sql += " order by s.StaffId desc";

        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;



    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {

        Response.Redirect("StaffAdd.aspx?moduid=" + hfxs.Value + "&action=add");
    }
    private void gdata()
    {

        string sql = "select s.StaffId,s.AuditState, s.StaffName,s.StaffWork,s.StaffTel,s.StaffPercentage,s.StaffPrice,s.StaffSalary,s.SWage,convert(char(10),s.WageDate,120) as WageDate,s.EditUserName,convert(char(10),s.EditDate,120) as EditDate,s.StaffOrder,w.WTitle,d.Deptname,(select count(0) from CS_Sales where s.StaffId=Saleman)as salesNum from CS_Staff as s,CS_WorkTitle as w,Department as d where w.WTId=s.StaffWork and d.Deptid=s.Deptid order by s.StaffId desc";

        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;

    }
    private void initData()
    {

        hfuserId.Value = Request.Cookies["user"].Values["id"].ToString();



    }
    protected void btnallflag_Click(object sender, EventArgs e)
    {
        ZSql allflag = new ZSql();
        int n = 0;
        if (this.GvDataStaff.Rows.Count > 0)
        {
            for (int i = 0; i < GvDataStaff.Rows.Count; i++)
            {
                CheckBox ck = GvDataStaff.Rows[i].Cells[8].FindControl("chkDelete") as CheckBox;
                if (ck.Checked)
                {
                    int flag = 1;
                    allflag.Open("update CS_Staff set AuditState=" + flag + " where StaffId=" + this.GvDataStaff.Rows[i].Cells[0].Text);
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('审核成功');", true);
                }
                else
                {
                    n++;
                }

                if (n == this.GvDataStaff.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }


            }
        }
    }
    private void initType1()
    {
        ZSql type = new ZSql();
        type.Open("select * from CS_WorkTitle where AuditState=1");
        StaffWork.DataSource = type.m_table;
        StaffWork.DataTextField = "WTitle";
        StaffWork.DataValueField = "WTId";
        StaffWork.DataBind();
        StaffWork.Items.Insert(0, new ListItem("—选择所属职务—", "0"));
        type.Close();
        ZSql Dep = new ZSql();
        Dep.Open("select * from Department");
        ddrDepartment.DataSource = Dep.m_table;
        ddrDepartment.DataTextField = "Deptname";
        ddrDepartment.DataValueField = "Deptid";
        ddrDepartment.DataBind();
        ddrDepartment.Items.Insert(0, new ListItem("—选择所属部门—", "0"));
        Dep.Close();

    }
    protected void GvDataStaff_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        f.checkLevels(Request.Cookies["user"].Values["roleid"], hfxs.Value, 12, GvDataStaff, btndel, btnallflag, btnAdd);
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string AuditState = DataBinder.Eval(e.Row.DataItem, "AuditState").ToString();

            if (AuditState == "1")
            {
                e.Row.Cells[10].Text = "在职";
                e.Row.Cells[10].ForeColor = System.Drawing.Color.FromName("#ff0000");
            }
            if (AuditState == "0")
            {
                e.Row.Cells[10].Text = "离职";
                e.Row.Cells[10].ForeColor = System.Drawing.Color.FromName("#cccccc");
            }
            if (AuditState == "")
            {
                e.Row.Cells[10].Text = "未知";
                e.Row.Cells[10].ForeColor = System.Drawing.Color.FromName("#cccccc");
            }
            string salesNum = DataBinder.Eval(e.Row.DataItem, "salesNum").ToString();
            if ( Convert.ToInt16(salesNum)>0)
            {
                (e.Row.FindControl("chkDelete") as CheckBox).Enabled = false;
            }

        }
    }
}
